package com.ly.algorithm.DataStructures.linkedlist;

/**
 * @Classname DoubleLinkedList
 * @Description TODO
 * @Date 2020/4/21 21:01
 * @Author 冷心影翼
 */
public class DoubleLinkedList<T> {
    private Node<T> head;

    public DoubleLinkedList() {
        this.head = new Node(null);
    }

    public void listReverse() {
        Node<T> node = head;
        while (true) {
            if(node.next == null) {
                break;
            }
            node = node.next;
        }
        while (true) {
            if(node == head) {
                break;
            }
            System.out.println(node);
            node = node.pre;
        }
    }

    public  void  list() {
        if(head.next == null) {
            System.out.println("链表为空");
            return;
        }
        Node<T> node = head.next;
        while(true) {
            if(node==null) {
                break;
            }
            System.out.println(node);
            node = node.next;
        }
    }

    public void add(Node<T> n) {
        Node<T> cur = head;
        while (true) {
            if(cur.next == null) {
                break;
            }
            cur = cur.next;
        }
        cur.next = n;
        n.pre = cur;
    }

    public void addByOrder(int index,Node<T> n) {
        if(index<=0 || index>getSize()) {
            System.out.println("超过最大长度");
        }
        Node<T> node = head;
        for(int i=0;i<index;i++) {
            node = node.next;
        }
        n.next = node;
        n.pre = node.pre;
        node.pre.next = n;
        node.pre = n;
    }

    public void update(int index,T data) {
        if(index<=0 || index>getSize()) {
            System.out.println("没有找到数据");
        }
        Node<T> node = head;
        for(int i=0;i<index;i++) {
            node = node.next;
        }
        node.data = data;
    }

    public Node<T> get(int index) {
        if(index<=0 || index>getSize()) {
            System.out.println("没有找到数据");
        }
        Node<T> node = head;
        for(int i=0;i<index;i++) {
            node = node.next;
        }
        return node;
    }

    public void delete(int index) {
        if(index<=0 || index>getSize()) {
            System.out.println("没有找到数据");
        }
        Node<T> node = head;
        for(int i=0;i<index;i++) {
            node = node.next;
        }
        node.pre.next = node.next;
        //删除最后一个节点的判断
        if(node.next!=null) {
            node.next.pre = node.pre;
        }

    }

    public int getSize() {
        if(head.next == null) {
            return 0;
        }
        Node temp = head;
        int size = 0;
        while (true) {
            if(temp.next==null) {
                break;
            }
            temp = temp.next;
            size ++;
        }
        return size;
    }
}

class Test2 {

    public static void main(String[] args) {
        DoubleLinkedList<String> doubleLinkedList = new DoubleLinkedList<>();

        doubleLinkedList.add(new Node<>("a"));
        doubleLinkedList.add(new Node<>("b"));
        doubleLinkedList.add(new Node<>("c"));
        System.out.println(doubleLinkedList.getSize());
//        doubleLinkedList.update(3,"dd");
        doubleLinkedList.delete(3);
//        doubleLinkedList.addByOrder(3,new Node<>("d"));
        doubleLinkedList.list();
        System.out.println("----------");
        doubleLinkedList.listReverse();
//        System.out.println(doubleLinkedList.get(3));
    }
}

class Node<T> {

    public T data;

    public Node<T> next;

    public Node<T> pre;

    public Node(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Node" +
                " data=" + data ;
    }
}
